﻿// Copyright (C) 2009  Lex Li
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace Lextm.M8Pack.ThemeBuilder
{
    internal class Model
    {
        private readonly ThemeBuilder _builder = new ThemeBuilder(Path.GetTempPath());
        private readonly MainScreenBuilder _main;
        // private readonly CallScreenBuilder _call;
        private readonly StandbyScreenBuilder _standby;

        public Model(DockPanel panel)
        {
            _standby = new StandbyScreenBuilder(panel);
            // _call = new CallScreenBuilder(panel);
            _main = new MainScreenBuilder(panel);
            _main.Theme = null;
            // _call.Theme = null;
            _standby.Theme = null;
        }

        public bool InUse
        {
            get 
            {
                return _builder.Theme != null;
            }
        }

        public void SaveTheme(string fileName)
        {
            using (Bitmap bitmap = _main.GetPreview())
            {
                _builder.Theme.AddPreview(bitmap);
            }

            _builder.SaveTheme(fileName);
        }

        public void LoadTheme(string fileName)
        {
            _builder.LoadTheme(fileName);
            _standby.Theme = _builder.Theme;
            // _call.Theme = _builder.Theme;
            _main.Theme = _builder.Theme;

            // ShowCallScreen();
            ShowStandbyScreen();
            ShowMainScreens();
        }

        public void ChangeAuthor()
        {
            if (_builder.Theme == null)
            {
                return;
            }

            using (FormChangeAuthor author = new FormChangeAuthor())
            {
                author.Author = _builder.Theme.Author;
                if (author.ShowDialog() == DialogResult.OK)
                {
                    _builder.Theme.Author = author.Author;
                }
            }
        }

        public void ShowMainScreens()
        {
            _main.Show();
        }

        public void ShowCallScreen()
        {
            // _call.Show();
        }

        public void ShowStandbyScreen()
        {
            _standby.Show();
        }

        public void AddIcon()
        {
            _main.AddIcon(_builder.Theme);
        }

        public void RemoveIcon()
        {
            _main.RemoveIcon();
        }

        internal void ChangeFont(Font font)
        {
            _main.ChangeFont(font);
        }

        public void SwitchMode()
        {
            _main.SwitchMode();
            // _call.SwitchMode();
            _standby.SwitchMode();
        }
    }
}
